//
//  AddYunDanOneVC.m
//  ReceiptDelivery
//
//  Created by 吕晶 on 2020/9/17.
//  Copyright © 2020 吕晶. All rights reserved.
//

#import "AddYunDanOneVC.h"
#import "AddYunDanStatusView.h"
#import "AddYunDanMainModel.h"
#import "AddYunDanOneCell.h"
#import "AddYunDanTwoVC.h"

@interface AddYunDanOneVC ()<UITableViewDelegate,UITableViewDataSource>{
    int page;
}

@property (nonatomic, strong) AddYunDanStatusView *statusView;
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) AddYunDanMainModel *addModel;

@end

@implementation AddYunDanOneVC

-(AddYunDanMainModel *)addModel {
    if (!_addModel) {
        _addModel = [AddYunDanMainModel new];
    }
    return _addModel;
}

-(AddYunDanStatusView *)statusView {
    if (!_statusView) {
        _statusView = [[AddYunDanStatusView alloc] initWithFrame:CGRectMake(0, 0, ScreenW, 75)];
        _statusView.status = 1;
    }
    return _statusView;
}

-(UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, self.statusView.nim_bottom, ScreenW, ScreenH - BottomHeight - NaviHeight - self.statusView.nim_height)];
        _tableView.backgroundColor = Color_backgroundColor;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        __weak_self;
//        _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
//            __strong_self;
//            strongSelf->page = 1;
//            [strongSelf requestForData];
//        }];
        _tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
            __strong_self;
            strongSelf->page++;
            [strongSelf requestForList];
        }];
    }
    return _tableView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"新建运单(中转)";
    page = 1;
    [self.view addSubview:self.statusView];
    [self.view addSubview:self.tableView];
    [self requestForList];
}

- (void)requestForList {
    if (self.wa_id.length) {
        [HttpRequest PostWithApi:ReceiptDeliveryEditYunDanWuLiuCompanyList params:@{@"page":@(page),@"wa_id":self.wa_id} progress:^(NSProgress * _Nonnull progress) {
            
        } success:^(NSURLSessionDataTask * _Nonnull operation, id  _Nonnull responseObject, BOOL code, NSInteger errorCode) {
            if (code) {
                if (self->page == 1) {
                    [self.addModel.wuLiuCompanyArray removeAllObjects];
                    self.addModel.wuLiuCompanyArray = [AddYunDanWuLiuCompanyModel mj_objectArrayWithKeyValuesArray:responseObject[@"list"]];
                }else {
                    NSArray *array = [AddYunDanWuLiuCompanyModel mj_objectArrayWithKeyValuesArray:responseObject[@"list"]];
                    [self.addModel.wuLiuCompanyArray addObjectsFromArray:array];
                }
                [self.addModel.wuLiuCompanyArray enumerateObjectsUsingBlock:^(AddYunDanWuLiuCompanyModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                    [obj.pl_warehouse enumerateObjectsUsingBlock:^(AddYunDanCangKuModel * _Nonnull wa_obj, NSUInteger wa_idx, BOOL * _Nonnull wa_stop) {
                        if (wa_obj.selected) {
                            obj.selectCangKu = wa_obj;
                            self.addModel.selectCompany = obj;
                        }
                    }];
                }];
                BOOL has_more = [responseObject[@"has_more"] boolValue];
                if (has_more) {
                    self.tableView.mj_footer.hidden = NO;
                }else {
                    self.tableView.mj_footer.hidden = YES;
                }
                [self.tableView reloadData];
            }
            [self.tableView.mj_header endRefreshing];
            [self.tableView.mj_footer endRefreshing];
        } fail:^(NSError * _Nonnull error) {
            [self.tableView.mj_header endRefreshing];
            [self.tableView.mj_footer endRefreshing];
        }];
    }else {
        [HttpRequest PostWithApi:ReceiptDeliveryAddYunDanWuLiuCompanyList params:@{@"page":@(page)} progress:^(NSProgress * _Nonnull progress) {
            
        } success:^(NSURLSessionDataTask * _Nonnull operation, id  _Nonnull responseObject, BOOL code, NSInteger errorCode) {
            if (code) {
                if (self->page == 1) {
                    [self.addModel.wuLiuCompanyArray removeAllObjects];
                    self.addModel.wuLiuCompanyArray = [AddYunDanWuLiuCompanyModel mj_objectArrayWithKeyValuesArray:responseObject[@"list"]];
                }else {
                    NSArray *array = [AddYunDanWuLiuCompanyModel mj_objectArrayWithKeyValuesArray:responseObject[@"list"]];
                    [self.addModel.wuLiuCompanyArray addObjectsFromArray:array];
                }
                BOOL has_more = [responseObject[@"has_more"] boolValue];
                if (has_more) {
                    self.tableView.mj_footer.hidden = NO;
                }else {
                    self.tableView.mj_footer.hidden = YES;
                }
                [self.tableView reloadData];
            }
            [self.tableView.mj_header endRefreshing];
            [self.tableView.mj_footer endRefreshing];
        } fail:^(NSError * _Nonnull error) {
            [self.tableView.mj_header endRefreshing];
            [self.tableView.mj_footer endRefreshing];
        }];
    }
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.addModel.wuLiuCompanyArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    AddYunDanOneCell *cell = [AddYunDanOneCell AddYunDanOneCellWithTableView:tableView];
    AddYunDanWuLiuCompanyModel *model = self.addModel.wuLiuCompanyArray[indexPath.row];
    cell.model = model;
    __weak_self;
    cell.selectBlock = ^{
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
        [model.pl_warehouse enumerateObjectsUsingBlock:^(AddYunDanCangKuModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            UIAlertAction *action1 = [UIAlertAction actionWithTitle:obj.name style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                weakself.addModel.wuLiuCompanyArray[indexPath.row].selectCangKu = obj;
                [weakself.tableView reloadData];
            }];
            [alert addAction:action1];
        }];
        UIAlertAction *cancle = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            
        }];
        [alert addAction:cancle];
        [weakself presentViewController:alert animated:YES completion:nil];
    };
    cell.sureBlock = ^{
        if (model.selectCangKu.w_id.length) {
            weakself.addModel.selectCompany = model;
            AddYunDanTwoVC *vc = [AddYunDanTwoVC new];
            vc.addModel = weakself.addModel;
            if (weakself.wa_id.length) {
                vc.wa_id = weakself.wa_id;
            }
            [weakself.rt_navigationController pushViewController:vc animated:YES];
        }else {
            [MBProgressHUD leShowFailedHUD:MAIN_WINDOW message:@"请先选择仓库"];
        }
    };
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    AddYunDanOneCell *cell = [AddYunDanOneCell AddYunDanOneCellWithTableView:tableView];
    return cell.height;
}

@end
